$(function () {
    $.extend({
        only: function (ar) {
            var ret = [],
                end;
            ar.sort();
            end = ar[0];
            ret.push(ar[0]);

            for (var i = 1; i < ar.length; i++) {
                if (ar[i] !== end) {
                    ret.push(ar[i]);
                    end = ar[i];
                }
            }
            return ret;
        }
    });
    var ls = window.localStorage;
    var ss = window.sessionStorage;
    var haveMaxCount = 100;//保留最大值
    var agree_key = 'is_agree_key';
    if (typeof ls === 'undefined' || typeof ss === 'undefined') {
        layer.msg('系统不支持localStorage或sessionStorage');
    }
    var isAgree = ss.getItem(agree_key);
    console.log(isAgree);
    console.log(typeof(isAgree));
    if (isAgree === null || parseInt(isAgree) === 0) {
        //弹出警告
        layer.confirm("本程序仅限于学习,禁止从事非法活动,本网站不承担任何直接或间接的责任<br/>后果自付!<br/>后果自付!!<br/>后果自付!!!",
            {btn: ['同意', '不同意'], icon: 7, shade: [0.9, '#000'], 'title':'警告!!!'},
            function (index) {
            ss.setItem(agree_key, 1);
            layer.close(index);
        }, function (index) {
            ss.setItem(agree_key, 0);
            layer.close(index);
            $("body").empty();
        });
    }

    //加载省,市,区
    new Welefen.Class.City("province", "city", "area");
    var year = $("#year");
    var myDate = new Date();
    var maxYear = myDate.getFullYear();
    var minYear = maxYear - 50;
    var opt = document.createElement("option");
    opt.innerHTML = opt.value = "请选择年份";
    year.append(opt);
    for (var val = maxYear; val >= minYear; val--) {
        var tmp = document.createElement("option");
        tmp.value = val;
        tmp.innerHTML = val + "年";
        year.append(tmp);
    }
    //月
    var opt = document.createElement("option");
    opt.innerHTML = opt.value = "请选择月份";
    var month = $("#month");
    month.append(opt);
    for (var val = 12; val >= 1; val--) {
        var tmp = document.createElement("option");
        val = val <= 9 ? '0' + val : val;
        tmp.value = val;
        tmp.innerHTML = val + "月";
        month.append(tmp);
    }
    //日
    var opt = document.createElement("option");
    opt.innerHTML = opt.value = "请选择日期";
    var day = $("#day");
    day.append(opt);
    for (var val = 1; val <= 31; val++) {
        var tmp = document.createElement("option");
        val = val <= 9 ? '0' + val : val;
        tmp.value = val;
        tmp.innerHTML = val + "日";
        day.append(tmp);
    }
    //number
    var number = $("#number");
    for (var val = 1; val <= 100; val++) {
        var tmp = document.createElement("option");
        val = val <= 9 ? '0' + val : val;
        tmp.value = val;
        tmp.innerHTML = val;
        number.append(tmp);
    }
    //提交
    var generate_key = 'history_generate_key';
    $("#submit").click(function () {
        var data = $("#cardForm").serialize();
        $.post('action.php?act=generate', data, function (rev) {
            if (rev.status === 0) {
                $("#panel-100 .result").empty();
                $.each(rev.data, function (index, val) {
                    $("#panel-100 .result").append("<li>" + val + "</li>");
                });
                var local = ls.getItem(generate_key);
                var list = [];
                if (local === null) {
                    list = rev.data;
                } else {
                    list = JSON.parse(local);
                    list = list.concat(rev.data);
                }
                if (list.length > haveMaxCount) {
                    var diff = list.length - haveMaxCount;
                    list.splice(0, diff);
                }
                ls.setItem(generate_key, JSON.stringify(list));
            } else {
                layer.alert(rev.msg);
            }
        }, 'json');
    });
    //读取历史记录
    var generate_history = ls.getItem(generate_key);
    if (generate_history !== null) {
        var list = JSON.parse(generate_history);
        $.each(list, function (index, val) {
            $("#panel-100 .history").append("<li>" + val + "</li>");
        })
    }
    //查询
    var select_key = 'history_select_card';
    $("#panel-101 .submit").click(function () {
        var card = $("#check_card").val();
        $.post('action.php?act=check_card', {card: card}, function (rev) {
            if (rev.status !== 0) {
                layer.alert(rev.msg);
            } else {
                $("#panel-101 .result").empty();
                $.each(rev.data, function (index, val) {
                    $("#panel-101 .result").append("<li>" + val + "</li>");
                });
                var local = ls.getItem(select_key);
                var list = [];
                if (local === null) {
                    list.push(card)
                } else {
                    list = JSON.parse(local);
                    list.push(card)
                }
                if (list.length > haveMaxCount) {
                    var diff = list.length - haveMaxCount;
                    list.splice(0, diff);
                }
                list = $.only(list);
                console.log(list);
                ls.setItem(select_key, JSON.stringify(list));
            }
        }, 'json');
    });
    //读取历史记录
    var history = ls.getItem(select_key);
    if (typeof history !== 'undefined') {
        var list = JSON.parse(history);
        $.each(list, function (index, val) {
            $("#panel-101 .history").append("<li>" + val + "</li>");
        })
    }

});
